package control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/shop/getGoods")
public class GetGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        List<String> goods=(List<String>)session.getAttribute("goods");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charst=utf-8");
        PrintWriter out=resp.getWriter();
        if (goods==null){
            out.write("{\"goods\":"+null+"}");
        }
        else {
            Object[] arr=goods.toArray();
           String s="{\"goods\":[";
            for (int i=0;i<arr.length;i++){
                s+="\""+arr[i]+"\"";
                if (i<arr.length-1)
                    s+=",";
            }
            s+="]}";
            out.write(s);
        }
    }
}
